package com.chy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.common.BaseContext;
import com.chy.common.R;
import com.chy.common.SnowflakeIdUtils;
import com.chy.entity.User;
import com.chy.mapper.UserInfoMapper;
import com.chy.mapper.UserMapper;
import com.chy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

//    public User getOneById(Long id) {
//        return userMapper.selectById(id);
//    }

    @Override
    public Page selectByPage(Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer pageSize = (Integer) params.get("pageSize");
        String name = (String) params.get("name");
        Page pageInfo = new Page(page, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), "user.name", name)
                .eq("user.deleted", 0)
                .orderByDesc("user.sort")
                .orderByDesc("user.create_time");

//        userMapper.selectMapsPage(pageInfo, wrapper);
        userMapper.selectMyPage(pageInfo, wrapper);
        return pageInfo;
    }

    @Transactional
    public R<String> createNewUser(Map<String, Object> params) {

        List<Map<String, Object>> maps = userMapper.selectRepeatUsers(params);
        if(maps.size() > 0) {
            return R.error("用户名或邮箱已被使用！");
        }
        String password = (String) params.get("password");
        params.put("password", DigestUtils.md5DigestAsHex(password.getBytes()));
        long snowId = new SnowflakeIdUtils(1, 1).nextId();
        params.put("id", snowId);
//        params.put("userId", snowId);
        params.put("createTime", LocalDateTime.now());
        params.put("updateTime", LocalDateTime.now());
        params.put("createUser", BaseContext.getCurrentId() != null ? BaseContext.getCurrentId() : 0);
        params.put("updateUser", BaseContext.getCurrentId() != null ? BaseContext.getCurrentId() : 0);

        boolean flag1 = userMapper.insertUser(params);
        boolean flag2 = userInfoMapper.insertUserInfo(params);
        return flag1 && flag2 ? R.success() : R.error();
    }

    /**
     * 同时修改user和userInfo
     * @param params
     * @return
     */
    @Override
    @Transactional
    public R<String> updateUser(Map<String, Object> params) {
        List<Map<String, Object>> maps = userMapper.selectRepeatUsers(params);
        if(maps.size() > 0) {
            return R.error("用户名或邮箱已被使用！");
        }
        params.put("updateTime", LocalDateTime.now());
        params.put("updateUser", BaseContext.getCurrentId() != null ? BaseContext.getCurrentId() : 0);
        boolean flag1= userMapper.updateUser(params);
        boolean flag2 = userInfoMapper.updateUserInfo(params);
        return flag1 && flag2 ? R.success() : R.error();
    }

    public Boolean deleteByIds(List<Map<String, Object>> lists) {
        int i = userMapper.deleteBatchIds(lists);
        int j = userInfoMapper.deleteBatchIds(lists);
        return i > 0 && j > 0;
    }

    @Override
    public R<String> resetPassword(Map<String, Object> params) {
        String password = "123456";
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", params.get("id"));
        map.put("password", DigestUtils.md5DigestAsHex(password.getBytes()));
        return userMapper.updateUser(map) ? R.success() : R.error();
    }


}
